package com.yiidata.rpc.thrift;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * <pre>
 *
 * Created by IntelliJ IDEA.
 * User: zhenqin
 * Date: 14/11/27
 * Time: 15:33
 * To change this template use File | Settings | File Templates.
 *
 * </pre>
 *
 * @author zhenqin
 */
public class ThriftHelloWorldServer implements ThriftHelloWorldProtocol.Iface {


    /**
     * 日志类
     */
    private static Logger log = LoggerFactory.getLogger(ThriftHelloWorldServer.class);


    public ThriftHelloWorldServer() {
    }


    @Override
    public Message sayHello(User user) throws TException {
        System.out.println("exec thrift rpc, hello, " + user.getName());
        return new Message("hi, " + user.getName());
    }



    public static void main(String[] args)  throws Exception {
        String host = "0.0.0.0";
        int port = 8088;
        TBinaryProtocol.Factory protoFactory = new TBinaryProtocol.Factory(true, true);
        TServerTransport serverTransport = new TServerSocket(new InetSocketAddress(host, port));
        ThriftHelloWorldProtocol.Processor processor = new ThriftHelloWorldProtocol.Processor(new ThriftHelloWorldServer());
        TThreadPoolServer tr_server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)
                .protocolFactory(protoFactory));

        log.info("start thrift server at: " + host +":" + port);
        tr_server.serve();
    }

}
